﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LibraryMusicStore;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraEditors.ViewInfo;

namespace AppMusicStore
{
    public partial class frmIssue : DevExpress.XtraEditors.XtraForm
    {
        IRepository<Issue> issueRepository;

        public frmIssue()
        {
            InitializeComponent();
        }

        private void frmReceipt_Load(object sender, EventArgs e)
        {
            try
            {
                issueRepository = new Repository<Issue>();
                RetrieveData();
            }
            catch (Exception ex)
            {
                Common.ShowErrorBox(ex.Message);
            }
        }

        private void RetrieveData()
        {
            bindingSource1.DataSource = issueRepository.GetAll()
                .OrderByDescending(i => i.IssueId);
                //Common.GetIssues(db);
        }

        private void Editor_Click(object sender, EventArgs e)
        {
            try
            {
                frmIssueEditor frm = new frmIssueEditor();
                frm.IssueRepository = this.issueRepository;

                if (sender.GetType().Name == "SimpleButton")
                    frm.Text = "Thêm phiếu xuất";
                else
                {
                    if (gridView1.GetFocusedRowCellValue("IssueId") == null)
                        return;
                    frm.Text = "Sửa phiếu xuất";
                    frm.id = Convert.ToInt32(gridView1
                        .GetFocusedRowCellValue("IssueId"));
                }

                if (frm.ShowDialog(this) == DialogResult.OK)
                {
                    issueRepository = new Repository<Issue>();
                    RetrieveData();
                }
                frm.Dispose();
            }
            catch (Exception ex)
            {
                Common.ShowErrorBox(ex.Message);
            }
            
        }

        private void btnDelete_Click()
        {
            try
            {
                DialogResult rs;
                Issue issue;

                if (gridView1.SelectedRowsCount > 1)
                {
                    int[] iRows = gridView1.GetSelectedRows();

                    rs = Common.ShowQuestionBox("Xóa các phiếu đã chọn?", "okcancel");

                    if (rs == DialogResult.OK)
                    {
                        foreach (var item in iRows)
                        {
                            issue = issueRepository.GetById(
                                Convert.ToInt32(gridView1
                                .GetRowCellValue(item, "IssueId")));

                            issueRepository.DeleteOnSubmit(issue);
                        }

                        issueRepository.SubmitChanges();

                        gridView1.DeleteSelectedRows();
                        gridControl1.Update();
                    }
                    return;
                }

                if (gridView1.GetFocusedRowCellValue("IssueId") == null)
                    return;

                issue = issueRepository.GetById(Convert.ToInt32(gridView1
                    .GetFocusedRowCellValue("IssueId")));
                    //Common.GetIssueById(Convert.ToInt32(gridView1.GetFocusedRowCellValue("IssueId")), db);

                rs = Common.ShowQuestionBox("Xóa phiếu xuất " + issue
                    .IssueId + " ?", "okcancel");

                if (rs == DialogResult.OK)
                {
                    //issues.Context.IssueDetails.DeleteAllOnSubmit(issue.IssueDetails);
                    //issueRepository.DeleteAllOnSubmit(issue.IssueDetails);
                    issueRepository.DeleteOnSubmit(issue);
                    issueRepository.SubmitChanges();
                    gridView1.DeleteSelectedRows();
                    gridControl1.Update();
                    MessageBox.Show("Đã xóa", "Thông báo", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                Common.ShowErrorBox(ex.Message);
            }

            //InitGridProducts();
        }

        #region "Context menu item"
        private void InsertToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.Editor_Click(this.btnInsert, new EventArgs());
            btnInsert.PerformClick();
        }

        private void EditToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Editor_Click(this.repositoryItemButtonEdit, new EventArgs());
        }

        private void DeleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnDelete_Click();
        }
        #endregion

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (gridView1.GetFocusedRowCellValue("IssueId") == null)
                ChangeVisibleToolTip(false);
            else
                ChangeVisibleToolTip(true);
        }

        private void ChangeVisibleToolTip(bool p)
        {
            EditToolStripMenuItem.Visible = p;
            DeleteToolStripMenuItem.Visible = p;
        }

        private void repositoryItemButtonEdit_Click(object sender, EventArgs e)
        {
            Editor_Click(this.repositoryItemButtonEdit, new EventArgs());
        }

        private void gridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (Control.ModifierKeys != Keys.None) return;

                var view = sender as GridView;

                if (view.SelectedRowsCount < 2) return;

                GridHitInfo hi = view.CalcHitInfo(new Point(e.X, e.Y));

                if (e.Button == MouseButtons.Left && hi.RowHandle >= 0 &&
                    hi.InRowCell && hi.Column.Name == "colDelete")
                {
                    btnDelete_Click();
                }
            }
            catch (Exception)
            {
                return;
            }
        }

        private void repositoryItemButtonDelete_Click(object sender, EventArgs e)
        {
            btnDelete_Click();
        }
        
    }
}